package com.ruoyi.web.controller.module.wx;

import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.property.PropertyFeeOrder;
import com.ruoyi.system.domain.module.wx.WxTemplate;
import com.ruoyi.system.domain.module.wx.WxUser;
import com.ruoyi.system.service.module.property.IPropertyFeeOrderService;
import com.ruoyi.system.service.module.wx.IWxTemplateService;
import com.ruoyi.system.service.module.wx.IWxUserService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 微信公众号的模板消息列Controller
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@Validated
@Api(value = "微信公众号的模板消息列控制器", tags = {"微信公众号的模板消息列管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/wx/template")
public class WxTemplateController extends BaseController {

    private final IWxTemplateService wxTemplateService;
	private final IPropertyFeeOrderService propertyFeeOrderService;
	private final IWxUserService wxUserService;

    /**
     * 查询微信公众号的模板消息列列表
     */
    @ApiOperation("查询微信公众号的模板消息列列表")
    @PreAuthorize("@ss.hasPermi('module:wxtemplate:list')")
    @GetMapping("/list")
    public TableDataInfo<WxTemplate> list(@Validated WxTemplate wxTemplate) {
        return PageUtils.buildDataInfo(wxTemplateService.page(PageUtils.buildPage()));
    }

    /**
     * 获取微信公众号的模板消息列详细信息
     */
    @ApiOperation("获取微信公众号的模板消息列详细信息")
    @PreAuthorize("@ss.hasPermi('module:wxtemplate:query')")
    @GetMapping("/{id}")
    public AjaxResult<WxTemplate> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return AjaxResult.success(wxTemplateService.getById(id));
    }



	/**
	 * 修改物业费订单
	 */
	@ApiOperation("发送物业费订单")
	@GetMapping("/sendMsg/{orderId}")
	public AjaxResult sendMsg(@NotNull(message = "主键不能为空") @PathVariable("orderId") Long orderId) {
		PropertyFeeOrder propertyFeeOrder = propertyFeeOrderService.findById(orderId);
		if(propertyFeeOrder != null){
			WxUser wxUser = wxUserService.findById(propertyFeeOrder.getUserId());
			if(wxUser!=null){
				wxTemplateService.sendWxMsgByPropertyFee(wxUser,propertyFeeOrder);
			}
		}
		return AjaxResult.success();
	}

    /**
     * 新增微信公众号的模板消息列
     */
    @ApiOperation("新增微信公众号的模板消息列")
    @PreAuthorize("@ss.hasPermi('module:wxtemplate:add')")
    @Log(title = "微信公众号的模板消息列", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody WxTemplate wxTemplate) {
        return toAjax(wxTemplateService.save(wxTemplate) ? 1 : 0);
    }

    /**
     * 修改微信公众号的模板消息列
     */
    @ApiOperation("修改微信公众号的模板消息列")
    @PreAuthorize("@ss.hasPermi('module:wxtemplate:edit')")
    @Log(title = "微信公众号的模板消息列", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody WxTemplate wxTemplate) {
        return toAjax(wxTemplateService.updateById(wxTemplate) ? 1 : 0);
    }

    /**
     * 删除微信公众号的模板消息列
     */
    @ApiOperation("删除微信公众号的模板消息列")
    @PreAuthorize("@ss.hasPermi('module:wxtemplate:remove')")
    @Log(title = "微信公众号的模板消息列" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(wxTemplateService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
